package cn.iocoder.yudao.module.call.controller.admin.ivrcallrecords;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivrcallrecords.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrcallrecords.IvrCallRecordsDO;
import cn.iocoder.yudao.module.call.service.ivrcallrecords.IvrCallRecordsService;

@Tag(name = "管理后台 - ivr分段通话记录")
@RestController
@RequestMapping("/call/ivr-call-records")
@Validated
public class IvrCallRecordsController {

    @Resource
    private IvrCallRecordsService ivrCallRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建ivr分段通话记录")
    public CommonResult<Long> createIvrCallRecords(@Valid @RequestBody IvrCallRecordsSaveReqVO createReqVO) {
        return success(ivrCallRecordsService.createIvrCallRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr分段通话记录")
    @PreAuthorize("@ss.hasPermission('call:ivr-call-records:update')")
    public CommonResult<Boolean> updateIvrCallRecords(@Valid @RequestBody IvrCallRecordsSaveReqVO updateReqVO) {
        ivrCallRecordsService.updateIvrCallRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr分段通话记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-call-records:delete')")
    public CommonResult<Boolean> deleteIvrCallRecords(@RequestParam("id") Long id) {
        ivrCallRecordsService.deleteIvrCallRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr分段通话记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-call-records:query')")
    public CommonResult<IvrCallRecordsRespVO> getIvrCallRecords(@RequestParam("id") Long id) {
        IvrCallRecordsDO ivrCallRecords = ivrCallRecordsService.getIvrCallRecords(id);
        return success(BeanUtils.toBean(ivrCallRecords, IvrCallRecordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr分段通话记录分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-call-records:query')")
    public CommonResult<PageResult<IvrCallRecordsRespVO>> getIvrCallRecordsPage(@Valid IvrCallRecordsPageReqVO pageReqVO) {
        PageResult<IvrCallRecordsDO> pageResult = ivrCallRecordsService.getIvrCallRecordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrCallRecordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr分段通话记录 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-call-records:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrCallRecordsExcel(@Valid IvrCallRecordsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrCallRecordsDO> list = ivrCallRecordsService.getIvrCallRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr分段通话记录.xls", "数据", IvrCallRecordsRespVO.class,
                        BeanUtils.toBean(list, IvrCallRecordsRespVO.class));
    }

}